import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np

# 中文字体支持
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


# ===== 1. 读取数据 =====
matfile = r"D:\project\muscle_net\dino\S1_A1_E3.mat"
data = sio.loadmat(matfile)

# 查看变量名
print(data.keys())

# 取出信号和标签
emg = data['emg']        # (N, 10)
stimulus = data['stimulus'].squeeze()

# ===== 2. 基本参数 =====
fs = 100          # 采样率 Hz（NinaPro DB1/DB2）
duration = 25       # 可视化时长 s
samples = fs * duration

t = np.arange(samples) / fs

# ===== 3. 可视化 EMG 10通道 =====
fig, axes = plt.subplots(11, 1, figsize=(12, 10), sharex=True)

for ch in range(10):
    axes[ch].plot(t, emg[:samples, ch], linewidth=0.7)
    axes[ch].set_ylabel(f'Ch{ch+1}')
    if ch == 0:
        axes[ch].set_title('前 5 秒 EMG 信号（10 通道）')

# ===== 4. 可视化 stimulus 标签 =====
axes[10].step(t, stimulus[:samples], where='post', linewidth=1.5)
axes[10].set_ylabel('动作')
axes[10].set_xlabel('时间 (s)')

plt.tight_layout()
plt.show()
